package com.ljx.NoteBody.config;

import com.ljx.NoteBody.common.JacksonObjectMapper;
import com.ljx.NoteBody.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * 拓展mvc消息转化器
     * Java对象与json的相互转化
     */

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 创建消息转化器对象
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        // 设置对象转化器，底层使用jackson将Java对象转为json
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        // 将上面的消息转化器添加到mvc框架的转化器集合中，数字越小表示越靠前优先使用
        converters.add(0, messageConverter);

    }

    /**
     * 实现拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TokenInterceptor())
                .addPathPatterns("/api/upload", "/api/comment", "/api/note", "/api/agree/**");
//                .excludePathPatterns("/api/user/login", "/api/user/register");
    }

}
